#include"stdafx.h"
#include<iostream>
#include"SHADER.h"


const GLuint WIDTH = 800, HEIGHT = 600;

int main()
{
	if (!glfwInit()) 
	{
		std::cout << "ERROR:INITGLFW::ERROR" << std::endl;
	}
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
	if (!window)
	{
		std::cout << "Create Window error" << std::endl;
	}
	GLenum error0 = glGetError();
	if (error0 != GL_NO_ERROR)
	{
		std::cout << "OpenGL Error0: " << error0 << std::endl;
	}
	glfwMakeContextCurrent(window);

	glewExperimental = GL_FALSE;
	GLenum error = glGetError();

	if (error != GL_NO_ERROR)
	{
		std::cout << "OpenGL Error: " << error << std::endl;
	}

	GLenum err =  glewInit();

	if (GLEW_OK != err)
	{
		std::cout << "ERROR:" << glewGetErrorString(err) << std::endl;
	}

	glViewport(0, 0, WIDTH, HEIGHT);

	Shader ourShader("default.vs","default.frag");

	GLfloat vertices[] = {
	  0.5f,-0.5f,0.0f,  1.0f,0.0f,0.0f,
	  -0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f,
	  0.0f,0.5f,0.0f,   0.0f,0.0f,1.0f
	};

	GLuint VAO, VBO;
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);
		glGenBuffers(1, &VBO);
		glBindBuffer(GL_ARRAY_BUFFER, VBO);
		glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
		//Position attrib
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
		glEnableVertexAttribArray(0);
		//Color attrib
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
		glEnableVertexAttribArray(1);
	glBindVertexArray(0);

	while (!glfwWindowShouldClose(window)) {
		glfwPollEvents();

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		ourShader.Use();
		glBindVertexArray(VAO);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		glBindVertexArray(0);

		glfwSwapBuffers(window);
	}

	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);

	glfwTerminate();
	return 0;
}



